package com.qiaoba.file.aliyun.config;

import cn.hutool.extra.spring.SpringUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * 阿里云文件存储配置
 *
 * @author ailanyin
 * @date 2023-09-26 14:39:46
 */
@Configuration
@ComponentScan("com.qiaoba.file.aliyun")
public class AliyunConfig {

    public static String endpoint;

    @Value("${aliyun.oss.endpoint}")
    private void setEndpoint(String url) {
        endpoint = url;
    }

    @Value("${aliyun.oss.accessKey}")
    private String accessKey;

    @Value("${aliyun.oss.secretKey}")
    private String secretKey;

    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKey, secretKey);
    }

    @PreDestroy
    public void close() {
        SpringUtil.getBean(OSS.class).shutdown();
    }
}
